import React from 'react';

import Dialog from './../Dialog';

import Content from './content';

interface Item {
    message: string
}

type Type = 'warm' | 'success';

interface Option extends Item {
    line?:number,
    autoClose?: boolean,
    onPress?: Function
}


const _show = (type: Type, opt: Option) => {
    const {
        line,
        autoClose = true
    } = opt || {};

    const _style = {
        bottom: `auto`
    }

    const props: any = {
        line:line,
        fixed:false,
        style: _style,
        direction: 'top',
        maskOpacity: 0,
        enableBack:false
    }

    let timer: any = null;

    const closeFn = Dialog.show(
        <Content
            {...opt}
            type={type}
            onClose={() => {
                closeFn();
                if (!!timer) {
                    clearTimeout(timer)
                }
            }}
        />, props
    )

    if (autoClose) {
        timer = setTimeout(closeFn, 3500)
    }
}

const warm = (opt: Option) => _show('warm', opt);
const success = (opt: Option) => _show('success', opt)

export default {
    warm: warm,
    success: success
}
